package com.coldworks.base.manager;

import java.util.Locale;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;

import com.coldworks.coldjoke.R;

/**
 * 本地配置管理器
 * 
 * @since 2012-07-07
 */
public class BaseConfManager {
	protected final Logger log = LoggerFactory.getLogger(this.getClass());
	private static BaseConfManager instance = null;
	private int versionCode = 0;
	private String versionName = "0";
	private String language;
	private String country;
	private String channel;

	public static BaseConfManager getInstance() {
		if (instance == null) {
			synchronized (BaseConfManager.class) {
				if (instance == null) {
					instance = new BaseConfManager();
				}
			}
		}
		return instance;
	}

	private BaseConfManager() {
	}

	public String getLanguage() {
		try {
			this.language = Locale.getDefault().getLanguage();
		} catch (Exception e) {
			log.error("",e);
		}
		return language;
	}

	public String getCountry() {
		try {
			this.country = Locale.getDefault().getCountry();
		} catch (Exception e) {
			log.error("",e);
		}
		return country;
	}

	public String getChannel(Context context) {
		try {
			this.channel = getMetaData(context, "UMENG_CHANNEL");
		} catch (Exception e) {
			log.error("",e);
		}
		return channel;
	}

	public int getVersionCode(Context context) {
		try {
			PackageInfo pi = context.getPackageManager().getPackageInfo(
					context.getApplicationContext().getPackageName(), 0);
			this.versionCode = pi.versionCode;
		} catch (Exception e) {
			log.error("",e);
		}
		return versionCode;
	}

	public String getVersionName(Context context) {
		try {
			PackageInfo pi = context.getPackageManager().getPackageInfo(
					context.getApplicationContext().getPackageName(), 0);
			this.versionName = pi.versionName;
		} catch (Exception e) {
			log.error("",e);
		}
		return versionName;
	}

	public String getMetaData(Context context, String key) {
		try {
			ApplicationInfo ai = context.getPackageManager()
					.getApplicationInfo(context.getPackageName(),
							PackageManager.GET_META_DATA);
			String value = String.valueOf(ai.metaData.get(key));
			return value;
		} catch (Exception e) {
			log.error("",e);
			return null;
		}
	}

	public String getAppName(Context context) {
		return context.getResources().getString(R.string.app_name);
	}
}
